/*
 * StartField.java
 *
 * Created on 2007. mC!rcius 12., 16:50
 *
 */

package laborhazi;

/**
 * The field, from where the lemmings come
 * @author nyuszik
 */
public class StartField extends FieldElement {
    
    private Field putTo;
    private int iterations;
    private int[] toPos;
     /** Creates a new instance of StartField
      * @param allIterations the number of lemmings, who spawn here
      * @param stage the Field
      */
    public StartField(int allIterations, Field stage) {
        putTo = stage;
        iterations = allIterations;
        position = new int[2];
        toPos = new int[2];
    }
    public String toString() {
    	return("new startfield: " + position[0] + " " + position[1]);
    }
    /** 
     * Begins spawning
     */
    public void spawn() {
        int position[] = new int[2];
        
        for (; 0 < iterations; iterations--) {
            putTo.put(new Lemming(putTo), position);
        }
  
    }
    public void setPos(int[] poss) {
    	position[0] = poss[0];
     	position[1] = poss[1];
     }
    public void update(boolean b) {
    	
    	if (iterations > 0) {
    		right();
    		if ( !( (toPos) == null)) {
				/* put to field and increase lemming number AND decrease
				 * iterations here in startfield */
				putTo.put(new Lemming(putTo.getLemmingNum() + 1, putTo, 1), toPos);
				putTo.incLemming();
				--iterations;
			}else {
				left();
	    		if ( !( (toPos) == null) ) { 
					/* put to field and increase lemming number AND decrease
					 * iterations here in startfield */
					putTo.put(new Lemming(putTo.getLemmingNum() + 1, putTo, 0), toPos);
					putTo.incLemming();
					--iterations;
				} else {
					up();
					if ( !( (toPos) == null)) {
						/* put to field and increase lemming number AND decrease
						 * iterations here in startfield */
						putTo.put(new Lemming(putTo.getLemmingNum() + 1, putTo, 1), toPos);
						putTo.incLemming();
						--iterations;
					} else {
						down();
						if ( !( (toPos) == null)) {
							/* put to field and increase lemming number AND decrease
							 * iterations here in startfield */
							putTo.put(new Lemming(putTo.getLemmingNum() + 1, putTo, 1), toPos);
							putTo.incLemming();
							--iterations;
						}
					}
				}
				
			}
    	}
    	
    }
    /**
     * checks element up and return null if there is a fieldelement OR
     * out of map boundries ; return position else
     */
    private int[] up() {
    	toPos = new int[2];
    	/* set position up */
    	toPos[0] = position[0];
    	toPos[1] = position[1] + 1;
    	if (null == putTo.check(toPos)) { //checks position
    		/* returns position if there is nothing*/
    		return toPos;
    	} else {
    		toPos = null;
    		return null;
    	}
    }
    /**
     * checks element down and return null if there is a fieldelement OR
     * out of map boundries ; return position else
     */
    private int[] down() {
    	toPos = new int[2];
    	/* set position down */
    	toPos[0] = position[0];
    	toPos[1] = position[1] - 1;
    	if (null == putTo.check(toPos)) {
    		/* returns position if there is nothing*/
    		return toPos;
    	} else {
    		toPos = null;
    		return null;
    	}
    	
    }
    /**
     * checks element left and return null if there is a fieldelement OR
     * out of map boundries ; return position else
     */
    private int[] left() {
    	toPos = new int[2];
    	/* set position left */
    	toPos[0] = position[0] - 1;
    	toPos[1] = position[1];
    	if (null == putTo.check(toPos)) {
    		/* returns position if there is nothing*/
    		return toPos;
    	} else {
    		toPos = null;
    		return null;
    	}
    	
    }
    /**
     * checks element right and return null if there is a fieldelement OR
     * out of map boundries ; return position else
     */
    private int[] right() {
    	toPos = new int[2];
    	/* set position right */
    	toPos[0] = position[0] + 1;
    	toPos[1] = position[1];
    	if (null == putTo.check(toPos)) {
    		/* returns position if there is nothing*/
    		return toPos;
    	} else {
    		toPos = null;
    		return null;
    		}
    	
    }
    public boolean isBlocker() {
    	return true;
    }
    
    
    
    
    
    
    
    
}
